package com.software.sqlite.api.admin.v1.controller;

import com.alibaba.fastjson.JSON;
import com.software.sqlite.api.admin.v1.vo.AdminLoginUserVO;
import com.software.sqlite.api.admin.v1.vo.AdminV1Result;
import com.software.sqlite.entity.Page;
import com.software.sqlite.entity.TbMovieServer;
import com.software.sqlite.service.impl.TbMovieServerService;
import com.software.sqlite.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(value = "管理端-服务配置", tags = "管理端-服务配置")
@RestController
@Slf4j
@RequestMapping("/admin/v1/movieServer")
@CrossOrigin
public class MovieServerController extends BaseController {
    @Resource
    private TbMovieServerService movieServerService;

    @ApiOperation(value = "分页", notes = "分页")
    @GetMapping("/page")
    public AdminV1Result list(HttpServletRequest request, String token, Integer page, Integer limit, String keyWork, Integer shipId)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        AdminLoginUserVO loginUser= (AdminLoginUserVO) result.getData();
        TbMovieServer sysUser=new TbMovieServer();
        if(CommonUtils.isNotBlank(keyWork)){
            sysUser= JSON.parseObject(keyWork, TbMovieServer.class);
        }
        if(shipId!=null && !shipId.equals(0L))
        {
            sysUser.setShipId(shipId);
        }
        if(!loginUser.getUserType().equals("1") && CommonUtils.isNotBlank(loginUser.getPowerShipIds()) && !loginUser.getPowerShipIds().equals("0"))
        {
            sysUser.setPowerShipIds(loginUser.getPowerShipIds());
        }
        Page pageVO=movieServerService.selectPage(page,limit,sysUser);
        return  AdminV1Result.success(pageVO.getList(),pageVO.getTotalSize());
    }

    @ApiOperation(value = "列表", notes = "列表")
    @GetMapping("/list")
    public AdminV1Result<List<TbMovieServer>> list(String token,String userName,String trueName,String mobile)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        TbMovieServer model=new TbMovieServer();
        return  AdminV1Result.success(movieServerService.selectList(model));
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/detail")
    public AdminV1Result detail(String token,Long id)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        return  AdminV1Result.success(movieServerService.get(id));
    }

    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping("/add")
    public AdminV1Result add(@RequestBody TbMovieServer model)throws Exception{
        AdminV1Result result=getAdmin(model.getToken());
        if(result.getCode()==-1){ return result; }
        boolean res=movieServerService.edit(model);
        if(res)
        {
            return AdminV1Result.success();
        }else{
            return AdminV1Result.fail("用户名已存在");
        }

    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
    public AdminV1Result update(@RequestBody TbMovieServer model)throws Exception{
        AdminV1Result result=getAdmin(model.getToken());
        if(result.getCode()==-1){ return result; }
        boolean res=movieServerService.edit(model);
        if(res)
        {
            return AdminV1Result.success();
        }else{
            return AdminV1Result.fail("修改失败");
        }
    }

    @ApiOperation(value = "删除", notes = "删除")
    @GetMapping("/del")
    public AdminV1Result del(String token,Long id)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        TbMovieServer model=movieServerService.get(id);
        if(model!=null){
            model.setUseStatus("deleted");
            movieServerService.update(model);
        }
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量添加", notes = "批量添加")
    @PostMapping("/addList")
    public AdminV1Result addList(String token,List<TbMovieServer> list)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        movieServerService.addList(list);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量修改", notes = "批量修改")
    @PostMapping("/updateList")
    public AdminV1Result updateList(String token,List<TbMovieServer> list)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        movieServerService.updateList(list);
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量删除", notes = "批量删除")
    @GetMapping("/delList")
    public AdminV1Result delList(String token,String ids)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        String[] array=ids.split(",");
        if(array.length>0){
            for(int i=0;i<array.length;i++){
                TbMovieServer model=movieServerService.get(CommonUtils.parseLongData(array[i]));
                if(model!=null){
                    model.setUseStatus("deleted");
                    movieServerService.update(model);
                }
            }
            return AdminV1Result.success();
        }
        return AdminV1Result.fail("删除失败");
    }

    @ApiOperation(value = "批量导入Excel", notes = "批量导入Excel")
    @PostMapping("/importData")
    public AdminV1Result importData(String token)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success();
    }

    @ApiOperation(value = "批量导出Excel", notes = "批量导出Excel")
    @PostMapping("/exportData")
    public AdminV1Result exportData(String token)throws Exception{
        AdminV1Result result=getAdmin(token);
        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success();
    }


}
